package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.CompanyDao;
import com.itheima.dao.store.CourseDao;
import com.itheima.domain.store.Company;
import com.itheima.domain.store.Course;
import com.itheima.factory.MapperFactory;
import com.itheima.service.store.CourseService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

/**
 * @program: Web综合案例
 * @author: DingYaDong
 * @create: 2021-06-24 14:51
 */
public class CourseServiceImpl implements CourseService {
    @Override
    public int save(Course course) {
        int save = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CourseDao courseDao = MapperFactory.getMapper(sqlSession, CourseDao.class);
            String uuid = UUID.randomUUID().toString();
            course.setId(uuid);
            save = courseDao.save(course);
            //提交事务
            TransactionUtil.commit(sqlSession);
            return save;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public int delete(Course course) {
        int del = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CourseDao courseDao = MapperFactory.getMapper(sqlSession, CourseDao.class);
            del = courseDao.delete(course.getId());
            //提交事务
            TransactionUtil.commit(sqlSession);
            return del;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public int update(Course course) {
        int up = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CourseDao courseDao = MapperFactory.getMapper(sqlSession, CourseDao.class);
            up = courseDao.update(course);
            //提交事务
            TransactionUtil.commit(sqlSession);
            return up;
        } catch (Exception e) {

            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public Course findById(String id) {
        Course byId = null;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CourseDao courseDao = MapperFactory.getMapper(sqlSession, CourseDao.class);
            byId = courseDao.findById(id);
            return byId;
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Course> findAll() {
        List<Course> courseList;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CourseDao courseDao = MapperFactory.getMapper(sqlSession, CourseDao.class);
            courseList = courseDao.findAll();
            return courseList;
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public PageInfo findAll(int page, int size) {
        List<Course> courseList;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CourseDao courseDao = MapperFactory.getMapper(sqlSession, CourseDao.class);
            //PageHelper设置分页必须出现在查询的前面
            PageHelper.startPage(page,size);
            //查询
            courseList = courseDao.findAll();
            PageInfo pageInfo = new PageInfo(courseList);
            return pageInfo;
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
